#include "Modbus32.h"
#include "sdram.h"
#include "usart.h"
#include "led.h"
#include "W5500.h"
#include "socket.h"
#include "Common.h"
#include "stmflash.h"

u8 RxTimeOut;		
u8 TxTimeOut;		
u16 *pSwapZone ;  						   	   
u32 TxCount;		
u32	RxCount;	      
u32	TxPosi;

u8 CommRxBuffer[RX_BUFFER_SIZE1];
u8 CommTxBuffer[TX_BUFFER_SIZE1];

CommData mCommData __attribute__((at(0XC0000000)));

//2022-3 li
ModuleLockStu moduleLockStutas;

extern u8 getRegisterVal(u32 addr,u32 *tempData);

//直接计算CRC32
//常用简式：0xEDB88320——RAR,ZIP所使用
//u32 CRC32,        //CRC32初值
//u8 *Buff,         //数组
//u32 Len)          //数组长度
u32 crc32(u8 *buff,u32 len)          //数组长度
{
	u32 crc32=0;					//符合标准的话，初值必须是0。自定义的话就无所谓了
	u32 i;
	crc32 = ~crc32;
	while(len--)
	{
		for(i=0;i<8;i++)
		{
			if(crc32 & 1)
					crc32 = (crc32 >> 1) ^ 0xEDB88320;
			else
					crc32 = (crc32 >> 1);
			if((*buff) & (1<<i))
					crc32 ^= 0xEDB88320;
		}
		buff++;//下一个字节
	}
	return (crc32 ^ (~0L));
}
														  
void beginSend(void)
{
	TxTimeOut = 5/TIMER_INTERVAL;      
}

void Send(void)         
{	
 
    u8 hwBuffer[SOCKET_BUFF];
	u32 num=0;
	u32 end = 0;
	num=TxCount/SOCKET_BUFF;
	if((TxCount%SOCKET_BUFF)!=0)
	{
		num++;
	}	
	for(u32 i=0;i<num;i++)
	{		
		if (i < (num - 1))
		{
			end = (u32)(SOCKET_BUFF);
		}
		else if (i == (num - 1))
		{
			end =(u32)(TxCount%SOCKET_BUFF);
			if(end==0)
			{
				end = (u32)(SOCKET_BUFF);
			}
		}
		for(u32 j=0;j<end;j++)
		{
			hwBuffer[j]=CommTxBuffer[i*SOCKET_BUFF+j];
			
		}
		sendto(0, hwBuffer,end, PcIp, PcPort); 		
	}
	
 
}

u8 getCoilVal(u32 addr,u8 *tempData)
{					   
	u8 result = 0;
	u32 tempAddr=0;
	u16 *pCoilPtr = 0;  
	u32 CoilAddr = 0;	
	u16  CoilBit  = 0;
	tempAddr = addr;  	
	CoilAddr = tempAddr/16;
	CoilBit  = tempAddr%16;
	pCoilPtr = &mCommData.CmdRegUnused;   	
	pCoilPtr+= CoilAddr;				
	if(*pCoilPtr&(1<<CoilBit))			
		*tempData	  = 1;
	else
		*tempData     = 0;	
	return result;
} 

u8 getInputVal(u32 addr,u8 *tempData)
{					   
	u8 result = 0;
	u32 tempAddr;
	u16 *pCoilPtr = 0;  
	u32 CoilAddr = 0;	
	u16  CoilBit  = 0; 
	tempAddr = addr;
	CoilAddr = tempAddr/16;
	CoilBit  = tempAddr%16;		
	pCoilPtr = &mCommData.GpioDinUnused;
	pCoilPtr+= CoilAddr;			
	if(*pCoilPtr&(1<<CoilBit))			
		*tempData	  = 1;
	else
		*tempData     = 0;	
	return result;
}

u8 setCoilVal(u32 addr,u8 tempData)
{
	u8 result = 0;
	u32 tempAddr;
	u16 *pCoilPtr = 0;
	u32 CoilAddr = 0;	
	u16  CoilBit  = 0;
	tempAddr = addr;		
	CoilAddr = tempAddr/16;
	CoilBit  = tempAddr%16;
	pCoilPtr = &mCommData.CmdRegUnused;				
	pCoilPtr+= CoilAddr;                
	if	(tempData) 						
	{
		*pCoilPtr|=1<<CoilBit;				
	}
	else                                
	{
		*pCoilPtr&=~(1<<CoilBit);			
	}				  
	return result;
}

u8 getRegisterVal(u32 addr,u32 *tempData)
{
	u8 result = 0;
	u32 tempAddr;
	tempAddr = addr;	   
    *tempData=pSwapZone[tempAddr];
	return result;
}

u8 setRegisterVal(u32 addr,u32 tempData)
{
	u8 result = 0;
	u32 tempAddr;        
	tempAddr = addr;  
    pSwapZone[tempAddr]=(u16)tempData;   
	return result;
} 

void readCoil(void)
{
	u32 addr;
	u32 tempAddr;
	u32 byteCount;
	u32 bitCount;
	u32 crcData;
	u32 position;
	u32 i,k;
	u8 tempData;
	u8  exit = 0;
	
	addr = ((u32)CommRxBuffer[6]<<24) +((u32)CommRxBuffer[7]<<16) +((u32)CommRxBuffer[8]<<8) + CommRxBuffer[9];								
	tempAddr = addr;
	bitCount = ((u32)CommRxBuffer[10]<<24) +((u32)CommRxBuffer[11]<<16) +((u32)CommRxBuffer[12]<<8) + CommRxBuffer[13];	
	byteCount = bitCount / 8;					
	if(bitCount%8 != 0)
		byteCount++;
	for(k=0;k<byteCount;k++)					
	{
		position = k + 10;
		CommTxBuffer[position] = 0;
		for(i=0;i<8;i++)
		{
			getCoilVal(tempAddr,&tempData);		
			CommTxBuffer[position] |= tempData << i;
			tempAddr++;
			if(tempAddr >= addr+bitCount)		
			{	
				exit = 1;
				break;
			}
		}
		if(exit == 1)
			break;
	}
	CommTxBuffer[0] = 'H';
	CommTxBuffer[1] = 'H';
	CommTxBuffer[2] = 0;
	CommTxBuffer[3] = IED_ADDRESS;
	CommTxBuffer[4] = 0;
	CommTxBuffer[5] = 0x01;
	CommTxBuffer[6] = byteCount>>24;
	CommTxBuffer[7] = byteCount>>16;
	CommTxBuffer[8] = byteCount>>8;
	CommTxBuffer[9] = byteCount;
	byteCount += 10;
	crcData = crc32(CommTxBuffer,byteCount);
	CommTxBuffer[byteCount] = crcData >> 24;
	byteCount++;
	CommTxBuffer[byteCount] = crcData >> 16;
	byteCount++;
	CommTxBuffer[byteCount] = crcData >> 8;
	byteCount++;
	CommTxBuffer[byteCount] = crcData & 0xff;
	byteCount++;
	CommTxBuffer[byteCount] = 'T';
	byteCount++;
	CommTxBuffer[byteCount] = 'T';
	TxCount = byteCount + 1;
	beginSend();
}

void readInput(void)
{
	u32 addr;
	u32 tempAddr;
	u32 byteCount;
	u32 bitCount;
	u32 crcData;
	u32 position;
	u32 i,k;
	u8 tempData;
	u8  exit = 0;
	
	addr = ((u32)CommRxBuffer[6]<<24) +((u32)CommRxBuffer[7]<<16) +((u32)CommRxBuffer[8]<<8) + CommRxBuffer[9];							
	tempAddr = addr;	
	bitCount = ((u32)CommRxBuffer[10]<<24) +((u32)CommRxBuffer[11]<<16) +((u32)CommRxBuffer[12]<<8) + CommRxBuffer[13];
	byteCount = bitCount / 8;					
	if(bitCount%8 != 0)
		byteCount++;
	for(k=0;k<byteCount;k++)					
	{
		position = k + 10;
		CommTxBuffer[position] = 0;
		for(i=0;i<8;i++)
		{
			getInputVal(tempAddr,&tempData);		
			CommTxBuffer[position] |= tempData << i;
			tempAddr++;
			if(tempAddr >= addr+bitCount)
			{	
				exit = 1;
				break;
			}
		}
		if(exit == 1)
			break;
	}
	CommTxBuffer[0] = 'H';
	CommTxBuffer[1] = 'H';
	CommTxBuffer[2] = 0;
	CommTxBuffer[3] = IED_ADDRESS;
	CommTxBuffer[4] = 0;
	CommTxBuffer[5] = 0x02;
	CommTxBuffer[6] = byteCount>>24;
	CommTxBuffer[7] = byteCount>>16;
	CommTxBuffer[8] = byteCount>>8;
	CommTxBuffer[9] = byteCount&0xff;
	byteCount += 10;
	crcData = crc32(CommTxBuffer,byteCount);
	CommTxBuffer[byteCount] = crcData >> 24;
	byteCount++;
	CommTxBuffer[byteCount] = crcData >> 16;
	byteCount++;
	CommTxBuffer[byteCount] = crcData >> 8;
	byteCount++;
	CommTxBuffer[byteCount] = crcData & 0xff;
	byteCount++;
	CommTxBuffer[byteCount] = 'T';
	byteCount++;
	CommTxBuffer[byteCount] = 'T';
	TxCount = byteCount + 1;
	beginSend();
}

void readRegisters(void)
{
	u32 addr;
	u32 tempAddr;
	u32 crcData;
	u32 readCount;	
	u32 byteCount;				 
	u32 i;
	u32 tempData = 0;

	addr = ((u32)CommRxBuffer[6]<<24) +((u32)CommRxBuffer[7]<<16) +((u32)CommRxBuffer[8]<<8) + CommRxBuffer[9];
	tempAddr = addr;	
	readCount = ((u32)CommRxBuffer[10]<<24) +((u32)CommRxBuffer[11]<<16) +((u32)CommRxBuffer[12]<<8) + CommRxBuffer[13];	
	byteCount = readCount * 2;					//20160622
	for(i=0;i<byteCount;i+=2,tempAddr++)		//20160622
	{
		getRegisterVal(tempAddr,&tempData);
		CommTxBuffer[i+10] =tempData >> 8;		//20160622
		CommTxBuffer[i+11] =tempData & 0xff;	//20160622
		//CommTxBuffer[i+12] = tempData >> 8;
		//CommTxBuffer[i+13] = tempData & 0xff;
	}
	CommTxBuffer[0] = 'H';
	CommTxBuffer[1] = 'H';
	CommTxBuffer[2] = 0;
	CommTxBuffer[3] = IED_ADDRESS;
	CommTxBuffer[4] = 0;
	CommTxBuffer[5] = 3;
	CommTxBuffer[6] = byteCount>>24;
	CommTxBuffer[7] = byteCount>>16;
	CommTxBuffer[8] = byteCount>>8;
	CommTxBuffer[9] = byteCount&0xff;
	byteCount += 10;
	crcData = crc32(CommTxBuffer,byteCount);
	CommTxBuffer[byteCount] = crcData >> 24;
	byteCount++;
	CommTxBuffer[byteCount] = crcData >> 16;
	byteCount++;
	CommTxBuffer[byteCount] = crcData >> 8;
	byteCount++;
	CommTxBuffer[byteCount] = crcData & 0xff;
	byteCount++;
	CommTxBuffer[byteCount] = 'T';
	byteCount++;
	CommTxBuffer[byteCount] = 'T';
	TxCount = byteCount + 1;
	beginSend();
}

void forceSingleCoil(void)
{
	u32 addr;
	u32 tempAddr;
	u8 tempData;
	u8  onOff;
	u32 i;

	addr = ((u32)CommRxBuffer[6]<<24) +((u32)CommRxBuffer[7]<<16) +((u32)CommRxBuffer[8]<<8) + CommRxBuffer[9];
	tempAddr = addr;	
	onOff = CommRxBuffer[10];	
	if(onOff == 0xff)		
	{	
		tempData = 1;
	}	
	else if(onOff == 0x00)	
	{	
		tempData = 0;
	}
	setCoilVal(tempAddr,tempData);
	for(i=0;i<RxCount;i++)
	{
		CommTxBuffer[i] = CommRxBuffer[i];
	}
	TxCount = RxCount;
	if(mCommData.CmdReg.OneBit.CmdScanLedStartPin)	//20190422 add
	{
		ScanLedStartPins();
		mCommData.CmdReg.OneBit.CmdScanLedStartPin=0;
	}
	beginSend();
	if(mCommData.CmdReg.OneBit.CmdScanStart||mCommData.CmdReg.OneBit.CmdScanAccessoryStart||mCommData.CmdReg.OneBit.CmdScanEmptyStartPins)
	{		
		mCommData.DevStauts=0;
		//mCommData.ScanCntH=0;
		//mCommData.ScanCntL=0;
		CompressDataIndex=0;		
		for(u32 i=0;i<PIN_DATA_TOTAL_COMPRESS_LEN;i++)				//压缩数据
		{		
			mCommData.PinDataTotalCompress[i]=0;				
		}
		for(u32 i=0;i<PIN_DATA_TOTAL_LEN;i++)				
		{
			mCommData.PinDataTotal[i].StartPinNum=0;
			for(u32 j=0;j<PIN_DATA_TOTAL_PIN_DATA_LEN;j++)				
			{
				mCommData.PinDataTotal[i].PinData[j]=0;
			}
		}		
		if(mCommData.CmdReg.OneBit.CmdScanStart)
		{
			StartPinArrayIndex=0;
			mCommData.ScanCntH++;			//????
		}
		if(mCommData.CmdReg.OneBit.CmdScanAccessoryStart)
		{
			AccessoryArrayIndex=0;
		}
		if(mCommData.CmdReg.OneBit.CmdScanEmptyStartPins)
		{
			EmptyStartPinArrayIndex=0;
		}
	}
}

void presetMultipleRegisters(void)
{
	u32 addr;
	u32 tempAddr;
	//u32 byteCount;
	u32 setCount;
	u32 crcData;
	u32 tempData;						   
	u32 i;	

	addr = ((u32)CommRxBuffer[6]<<24) +((u32)CommRxBuffer[7]<<16) +((u32)CommRxBuffer[8]<<8) + CommRxBuffer[9];
	tempAddr = addr; 	
	setCount = ((u32)CommRxBuffer[10]<<24) +((u32)CommRxBuffer[11]<<16) +((u32)CommRxBuffer[12]<<8) + CommRxBuffer[13];
	//byteCount = ((u32)CommRxBuffer[14]<<24) +((u32)CommRxBuffer[15]<<16) +((u32)CommRxBuffer[16]<<8) + CommRxBuffer[17];						   

	for(i=0;i<setCount;i++,tempAddr++)
	{
		//tempData = ((u16)CommRxBuffer[i*2+18]<<8) + CommRxBuffer[i*2+19];  
		tempData = ((u16)CommRxBuffer[i*2+18]<<8) + CommRxBuffer[i*2+19]; 
		setRegisterVal(tempAddr,tempData);
	}
	CommTxBuffer[0] = 'H';
	CommTxBuffer[1] = 'H';
	CommTxBuffer[2] = 0;
	CommTxBuffer[3] = IED_ADDRESS;
	CommTxBuffer[4] = 0;
	CommTxBuffer[5] = 16;
	CommTxBuffer[6] = addr >> 24;
	CommTxBuffer[7] = addr >> 16;
	CommTxBuffer[8] = addr >> 8;
	CommTxBuffer[9] = addr & 0xff;
	CommTxBuffer[10] = setCount >> 24;
	CommTxBuffer[11] = setCount >> 16;
	CommTxBuffer[12] = setCount >> 8;
	CommTxBuffer[13] = setCount & 0xff;
	crcData = crc32(CommTxBuffer,14);
	CommTxBuffer[14] = crcData >> 24;
	CommTxBuffer[15] = crcData >>16;
	CommTxBuffer[16] = crcData >> 8;
	CommTxBuffer[17] = crcData & 0xff;	
	CommTxBuffer[18] = 'T';	
	CommTxBuffer[19] = 'T';
	TxCount = 20;			   
	beginSend();
}
//O
void presetSingleRegister(void)
{
	u32 addr;
	u32 tempAddr;	     
	u32 crcData;
	u32 tempData;		 

	addr = ((u32)CommRxBuffer[6]<<24) +((u32)CommRxBuffer[7]<<16) +((u32)CommRxBuffer[8]<<8) + CommRxBuffer[9];
	tempAddr = addr;
	//tempData = ((u32)CommRxBuffer[10]<<24) +((u32)CommRxBuffer[11]<<16) +((u32)CommRxBuffer[12]<<8) + CommRxBuffer[13];
	tempData = ((u32)CommRxBuffer[10]<<8) +((u32)CommRxBuffer[11]);
	setRegisterVal(tempAddr,tempData);
	CommTxBuffer[0] = 'H';
	CommTxBuffer[1] = 'H';
	CommTxBuffer[2] = 0;
	CommTxBuffer[3] = IED_ADDRESS;//localAddr;
	CommTxBuffer[4] = 0;
	CommTxBuffer[5] = 06;
	CommTxBuffer[6] = addr >> 24;
	CommTxBuffer[7] = addr >> 16;
	CommTxBuffer[8] = addr >> 8;
	CommTxBuffer[9] = addr & 0xff;
	//CommTxBuffer[10] = tempData >> 24;
	//CommTxBuffer[11] = tempData >> 16;
	CommTxBuffer[10] = tempData >> 8;
	CommTxBuffer[11] = tempData & 0xff;
	crcData = crc32(CommTxBuffer,12);
	CommTxBuffer[12] = crcData >> 24;
	CommTxBuffer[13] = crcData >> 16;
	CommTxBuffer[14] = crcData >> 8;
	CommTxBuffer[15] = crcData & 0xff;
	CommTxBuffer[16] = 'T';	
	CommTxBuffer[17] = 'T';
	TxCount = 18;
	if((mCommData.EndPinNumMax!=UserUsedEndPinNumMax)||(mCommData.SignalSetTime!=LocalSignalSetTime))
	{
		u16 wa[2]={0,0};
		wa[0]=mCommData.EndPinNumMax;
		wa[1]=mCommData.SignalSetTime;
		STMFLASH_Write(FLASH_SAVE_ADDR,(u32*)&wa,sizeof(wa));	
		u16 ra[2]={0,0};
		STMFLASH_Read(FLASH_SAVE_ADDR,(u32*)&ra,sizeof(ra));
		if((mCommData.EndPinNumMax!=ra[0])||(mCommData.SignalSetTime!=ra[1]))
		{
			while(1);
		}
		LocalSignalSetTime=mCommData.SignalSetTime;
		LocalSignalWaitTime=LocalSignalSetTime&0x00ff;
		LocalSignalSetCnt=(LocalSignalSetTime>>8);
		
		UserUsedEndPinNumMax = mCommData.EndPinNumMax;	
		u32 div=UserUsedEndPinNumMax/128;
		u32 res=UserUsedEndPinNumMax%128;
		if(res)
		{
			div++;
		}	
		UserUsedEndPinIndexMax=div*16;				//最大的终止点index	
		UserUsedCardNumMax=div;						//最大板卡数量
	}
	beginSend();  	
}

void forceMultipleCoils()
{
    u32 addr;
	u32 tempAddr;
	u32 byteCount;
	u32 bitCount;    
	u32 crcData;
	u32 tempData;	
	u32 i,k;
	u8  exit = 0;
	
	addr = ((u32)CommRxBuffer[6]<<24) +((u32)CommRxBuffer[7]<<16) +((u32)CommRxBuffer[8]<<8) + CommRxBuffer[9];
	tempAddr = addr;	
	bitCount = ((u32)CommRxBuffer[10]<<24) +((u32)CommRxBuffer[11]<<16) +((u32)CommRxBuffer[12]<<8) + CommRxBuffer[13];       	
	byteCount = ((u32)CommRxBuffer[14]<<24) +((u32)CommRxBuffer[15]<<16) +((u32)CommRxBuffer[16]<<8) + CommRxBuffer[17];	    		
	for(k=0;k<byteCount;k++)
	{
		for(i=0;i<8;i++)
		{
			if((CommRxBuffer[18+k]>>i)&0x01)
			    tempData=1;
			else
			    tempData=0;			
			setCoilVal(tempAddr,tempData);					
			tempAddr++;
			if(tempAddr >= addr+bitCount)
			{	
				exit = 1;
				break;
			}
		}
		if(exit == 1)
			break;
	}
	CommTxBuffer[0] = 'H';
	CommTxBuffer[1] = 'H';
	CommTxBuffer[2] = 0;
	CommTxBuffer[3] = IED_ADDRESS;
	CommTxBuffer[4] = 0;
	CommTxBuffer[5] = 0x0f;
	CommTxBuffer[6] = addr >> 24;
	CommTxBuffer[7] = addr >> 16;
	CommTxBuffer[8] = addr >> 8;
	CommTxBuffer[9] = addr & 0xff;
	CommTxBuffer[10] = bitCount >> 24;
	CommTxBuffer[11] = bitCount >> 16;
	CommTxBuffer[12] = bitCount >> 8;
	CommTxBuffer[13] = bitCount & 0xff;
	crcData = crc32(CommTxBuffer,14);
	CommTxBuffer[14] = crcData >> 24;
	CommTxBuffer[15] = crcData >> 16;
	CommTxBuffer[16] = crcData >> 8;
	CommTxBuffer[17] = crcData & 0xff;
	CommTxBuffer[18] = 'T';	
	CommTxBuffer[19] = 'T';
	TxCount = 20;			   
	beginSend();	
}

void CheckCommModbus32(void)
{
    u32 crcData=0;
	u32 tempData=0;	
	if((CommRxBuffer[2]<<8) + CommRxBuffer[3]!=IED_ADDRESS) 	
		return ;	
	if(RxCount > 10)               						
	{      
	    switch((CommRxBuffer[4]<<8) + CommRxBuffer[5])			
		{
			case 1:
			case 2:
			case 3:
			case 5:
			case 6:
			    if(RxCount >= 18)  						
			    {
					if((CommRxBuffer[RxCount-1]=='T')&&(CommRxBuffer[RxCount-2]=='T'))
					{
						crcData = crc32(CommRxBuffer,RxCount-6);  
						if(crcData == (((u32)CommRxBuffer[RxCount-6]<<24)+((u32)CommRxBuffer[RxCount-5]<<16)+((u32)CommRxBuffer[RxCount-4]<<8)+CommRxBuffer[RxCount-3]))	
						{
							if(((CommRxBuffer[4]<<8) + CommRxBuffer[5]) == 1)      
							{
								readCoil();
							}
							else if(((CommRxBuffer[4]<<8) + CommRxBuffer[5]) == 2) 
							{
								readInput();	
							}
							else if(((CommRxBuffer[4]<<8) + CommRxBuffer[5]) == 3) 
							{
								readRegisters();
							}
							else if(((CommRxBuffer[4]<<8) + CommRxBuffer[5]) == 5) 
							{
								forceSingleCoil();							
							}
							else if(((CommRxBuffer[4]<<8) + CommRxBuffer[5]) == 6) 
							{
								presetSingleRegister();							
							}  
						}							 
					}
					RxCount = 0;					
			    }
			break;

			case 15://设置多个线圈
//			    tempData = CommRxBuffer[6];   //读取数据字节数
//				tempData += 9;	//数据个数
//				if(RxCount >= tempData)
//				{
//					crcData = crc16(CommRxBuffer,tempData-2);					
//					if(crcData == ((u16)CommRxBuffer[tempData-2]<<8)+ CommRxBuffer[tempData-1])
//					{
//						//forceMultipleCoils();     //没有动作处理						
//					}
//					RxCount = 0;
//				}
			break;

			case 16://设置多个寄存器			   
				tempData = (((u32)CommRxBuffer[10])<<24) + (((u32)CommRxBuffer[11])<<16) +(((u32)CommRxBuffer[12])<<8) +CommRxBuffer[13];
				tempData = tempData * 2;		//数据字节数
				tempData += 24; 				//整包字节数													    
				if(RxCount >= tempData)
				{
					crcData = crc32(CommRxBuffer,tempData-6); 	 
					if(crcData == (((u32)CommRxBuffer[tempData-6]<<24)+((u32)CommRxBuffer[tempData-5]<<16)+((u32)CommRxBuffer[tempData-4]<<8)+CommRxBuffer[tempData-3]))
					{
						presetMultipleRegisters();
					}
					RxCount = 0;
				}
			break;
			case 17://报告从机ID	
			break;
			default:
			break;
		}
	}
}
								    
void TaskCommTimer(void)
{
	if(RxTimeOut>0) 						//接收帧与帧之间的时间,每接收到一帧就重置这个超时值
	{	
		--RxTimeOut;				 			   
		if(RxTimeOut==0 && RxCount>0)   //判断通讯接收是否超时，最后一帧接收完毕且超时时间达到
		{		                            //数据包与数据包之前要求10ms以内把数据包接收完成，接收不完就丢弃
            CheckCommModbus32();	            //MODBUS接收命令解析执行  												 
			RxCount = 0;                  //将接收地址偏移寄存器清零
 
		}			
	}
	if(TxTimeOut>0)							//延时启动发送任务
	{
		--TxTimeOut;
		if(TxTimeOut==0)                    //延时5ms，时间到发送
			Send();                         //发送延时时间到，发送一个字节
	}
}
 
void NormalDataInit(void)
{
	mCommData.IpNum = 1;
	mCommData.EndPinNumMax=0;		
	mCommData.SignalSetTime =8;
	mCommData.HwVersions[0]=1;
	mCommData.HwVersions[1]=0;
	mCommData.HwVersions[2]=0;
	mCommData.HwVersions[3]=0;
	mCommData.HwVersions[4]=1;	
	mCommData.HwId[0]=0;
	mCommData.HwId[1]=0;
	mCommData.HwId[2]=0;
	
	mCommData.ConfigReg=0;
	mCommData.ConfigReg|=1<<AUTO_UPLOAD_AFTER_SCAN;
	mCommData.CheckChannelNum=8;
	mCommData.CheckVoltageThreshold=0;
	
	mCommData.CmdRegUnused=0;
	mCommData.CmdReg.AllBits=0;
	mCommData.CmdExecuteState=0;	
	mCommData.GpioDoutData.AllBits =0;
	
	mCommData.GpioDinUnused=0;
	mCommData.GpioDinData.AllBits=0;
	mCommData.EndPinDin=0;
	mCommData.GpioDinData.AllBits=0;	
	mCommData.ProbeEndPinNum=0;
	mCommData.ErrCode=0;	
	mCommData.DevStauts=0;
	mCommData.GpioDinDataMap=0;
	mCommData.ScanCntH=0;
	mCommData.ScanCntL=0;	
	
	for(u32 i=0;i<PIN_DATA_TOTAL_COMPRESS_LEN;i+=2)		//压缩数据
	{		
		mCommData.PinDataTotalCompress[i]=0;		
		mCommData.PinDataTotalCompress[i+1]=0;				
	}	
	for(u32 i=0;i<SKIP_END_PIN_DATA_LEN;i++)					//跳过数据
	{		
		mCommData.SkipEndPinData[i]=0x0000;	//
	}
	for(u32 i=0;i<START_PINS_ARR_LEN;i++)					//起始针
	{
		mCommData.StartPinsArray[i]=i+1;		
	}
	mCommData.StartPinsArray[2047]=0;	
	mCommData.OneLoopData.StartPinNum=0;	//单个单回路
	mCommData.OneLoopData.EndPinNum=0;
	for(u32 i=0;i<ONE_LOOP_DATAS_LEN;i++) 					//多个单回路
	{
		mCommData.OneLoopDatas[i].StartPinNum=0;
		mCommData.OneLoopDatas[i].EndPinNum=0;
	}
	for(u32 i=0;i<EMPTY_START_PINS_LEN;i++)					//空点数据
	{		
		mCommData.EmptyStartPinData[i]=0x0000;
	}	
	for(u32 i=0;i<LED_START_PINS_LEN;i++)					//LED数据//20190422 add
	{		
		mCommData.LedStartPinData[i]=0x0000;		
	}
	for(u32 i=0;i<SCAN_PROBE_END_PINS_LEN;i++)					//LED数据//20190605 add
	{		
		mCommData.ScanProbeEndPins[i]=0x0000;
	}
	for(u32 i=0;i<PIN_DATA_TOTAL_LEN;i++)					//非压缩数据
	{												
		mCommData.PinDataTotal[i].StartPinNum=0;
		for(u32 j=0;j<PIN_DATA_TOTAL_PIN_DATA_LEN;j++)
		{							
			mCommData.PinDataTotal[i].PinData[j]=0;
		}		
	}	
}

void CommDataInit(void)
{
#if DEBUG_DATA
	DebugDataInit();
#else
	NormalDataInit();
#endif	
}
void ModbusInit(void)
{
	pSwapZone = (void *)&mCommData;	
	CommDataInit();
}

//自动上传寄存器值，首地址，数量
void AutoUploadRegisters(u32 addr,u32 num)
{
	//u32 addr;
	u32 tempAddr;
	u32 crcData;
	u32 readCount;	
	u32 byteCount;				 
	u32 i;
	u32 tempData = 0;

	//addr = ((u32)CommRxBuffer[6]<<24) +((u32)CommRxBuffer[7]<<16) +((u32)CommRxBuffer[8]<<8) + CommRxBuffer[9];
	tempAddr = addr;	
	//readCount = ((u32)CommRxBuffer[10]<<24) +((u32)CommRxBuffer[11]<<16) +((u32)CommRxBuffer[12]<<8) + CommRxBuffer[13];
	readCount =num;
	byteCount = readCount * 2;					//20160622
	for(i=0;i<byteCount;i+=2,tempAddr++)		//20160622
	{
		getRegisterVal(tempAddr,&tempData);
		CommTxBuffer[i+10] =tempData >> 8;		//20160622
		CommTxBuffer[i+11] =tempData & 0xff;	//20160622
		//CommTxBuffer[i+12] = tempData >> 8;
		//CommTxBuffer[i+13] = tempData & 0xff;
	}
	CommTxBuffer[0] = 'H';
	CommTxBuffer[1] = 'H';
	CommTxBuffer[2] = 0;
	CommTxBuffer[3] = IED_ADDRESS;
	CommTxBuffer[4] = 0;
	CommTxBuffer[5] = 3;
	CommTxBuffer[6] = byteCount>>24;
	CommTxBuffer[7] = byteCount>>16;
	CommTxBuffer[8] = byteCount>>8;
	CommTxBuffer[9] = byteCount&0xff;
	byteCount += 10;
	crcData = crc32(CommTxBuffer,byteCount);
	CommTxBuffer[byteCount] = crcData >> 24;
	byteCount++;
	CommTxBuffer[byteCount] = crcData >> 16;
	byteCount++;
	CommTxBuffer[byteCount] = crcData >> 8;
	byteCount++;
	CommTxBuffer[byteCount] = crcData & 0xff;
	byteCount++;
	CommTxBuffer[byteCount] = 'T';
	byteCount++;
	CommTxBuffer[byteCount] = 'T';
	TxCount = byteCount + 1;
	beginSend();
}





